/**
 * @TypeMgr.as
 *
 * @author sodaChen mail:sujun10#21cn.com
 * @version 1.0
 * <br>Copyright (C), 2013 asFrame.com
 * <br>This program is protected by copyright laws.
 * <br>Program Name:ASFrame
 * <br>Date:2016-5-29
 */
package com.asframe.mgr
{
	import com.asframe.collections.maps.HashMap;
	import com.asframe.pattern.Singleton;
	import com.asframe.ref.Type;
	import com.asframe.utils.ClassUtils;

	/**
	 * 类的Type管理对象，主要是带了缓存功能。所以如果通过这里获取到了Type，不需要使用时，必须进行删除，否则会内存泄漏
	 * @author sodaChen
	 * Date:2016-5-29
	 */
	public class TypeMgr extends Singleton
	{
		private static var instance:TypeMgr;

		private var typeMap:HashMap;


		public function TypeMgr():void
		{
			typeMap = new HashMap();
		}
		/**
		 * 根据Class对象查找对对应的Type对象，如果为空则会创建一个Type实例，并存放起来
		 * @param clazzOrInst Class对象或者是实例
		 * @return
		 *
		 */
		public function findType(clazzOrInst:* ):Type
		{
			var clazz:Class = null;
			if(clazzOrInst is Class)
				clazz = clazzOrInst;
			else
				clazz = ClassUtils.forInstance(clazzOrInst);

			var type:Type = typeMap.get(clazz);
			if(type == null)
			{
				type = Type.forClass(clazz);
				typeMap.put(clazz,type);
			}
			return type;
		}

		public function removeType(clazzOrInst:* ):void
		{
			var clazz:Class = null;
			if(clazzOrInst is Class)
				clazz = clazzOrInst;
			else
				clazz = ClassUtils.forInstance(clazzOrInst);

			typeMap.remove(clazz);
		}

		public static function getInstance():TypeMgr
		{
			if(instance == null)
			{
				instance = new TypeMgr();
			}
			return instance;
		}
	}
}